package com.sak.vis;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sak.vis.domain.Result;
import com.sak.vis.service.ExternalClientService;
import com.sak.vis.service.ResultOptimizationService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	@Autowired
	private ExternalClientService onYoMoExternalClientService;

	@Autowired
	private ResultOptimizationService luceneResultOptimizationService;

	/**
	 * Simply selects the home view to render by returning its name.
	 * Automatically maps the request parameters to variables.
	 */
	@RequestMapping(value = "/findcontact", method = RequestMethod.GET)
	public String home(
			Locale locale,
			Model model,
			@RequestParam(value = "txtweb-mobile", defaultValue = "00000000000") String mobile,
			@RequestParam(value = "txtweb-message", defaultValue = "") String message) {

		try {
			logger.info("Accessing app using [" + mobile + "] and message [" + message + "]");
			
			List<Result> results = onYoMoExternalClientService
					.queryClient(
							"s/delhi/~/eye%20doctor%20shahdra%20delhi",
							"");
		/*	List<Result> optimizedResults = luceneResultOptimizationService
					.optimize("eye doctor shahdara delhi", results);*/
			
			model.addAttribute("results", results);
		} catch (Exception exception) {
			logger.error("Problem with the URL access: ", exception);
		}

		return "home";
	}

}
